<template>
  <div class="box2">
    <div class="title">
      <p>年龄比例</p>
      <img src="../../images/dataScreen-title.png" alt="" />
    </div>
    <!-- 图形图标的容器 -->
    <div class="charts" ref="charts"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
//引入echarts
import * as echarts from 'echarts'
import { ReqAgeData } from '@/api/screen'

let ageData = ref({
  middleNum: 27,
  oldNum: 0,
  teenagerNum: 18,
  youthNum: 54,
})
let charts = ref()
//组件挂载完毕初始化图形图标
onMounted(async () => {
  await getAgeData()
  await init()
})
const init = () => {
  let mychart = echarts.init(charts.value)
  //设置配置项
  let option = {
    tooltip: {
      trigger: 'item',
    },
    legend: {
      right: 20,
      top: 40,
      orient: 'vertical', //图例组件方向的设置
      textStyle: {
        color: 'white',
        fontSize: 16,
      },
    },
    emphasize: {
      label: {
        show: true,
        fontSize: 40,
        fontWeight: 'bold',
      },
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: ['40%', '85%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2,
        },
        label: {
          show: true,
          position: 'insideTop',
          color: 'white',
        },

        labelLine: {
          show: false,
        },
        data: [
          { value: ageData.value.teenagerNum, name: '青少年人数' },
          { value: ageData.value.youthNum, name: '青年人数' },
          { value: ageData.value.middleNum, name: '中年人数' },
          { value: ageData.value.oldNum, name: '老年人数' },
        ],
      },
    ],
    //调整图形图标的位置
    grid: {
      left: 0,
      top: 0,
      right: 0,
      bottom: 0,
    },
  }
  mychart.setOption(option)
}
const getAgeData = async () => {
  let res = await ReqAgeData()
  ageData.value.middleNum = res.data.middleNum
  ageData.value.oldNum = res.data.oldNum
  ageData.value.teenagerNum = res.data.teenagerNum
  ageData.value.youthNum = res.data.youthNum
}
</script>

<style scoped lang="scss">
.box2 {
  width: 100%;
  height: 100%;
  background: url(../../images/dataScreen-main-cb.png) no-repeat;
  background-size: 100% 100%;

  .title {
    margin-left: 20px;

    p {
      color: white;
      font-size: 20px;
    }
  }

  .charts {
    height: 260px;
  }
}
</style>
